<?php
class EventManager
{
	public $user_id;
	public $from;
	public $to;
	public $search_text;

	
	public function get()
	{
		$db=database();
		$query ="
			SELECT events.* 
			FROM events
			WHERE TRUE";
		if ($this->user_id)
		{
			$query .="
			AND events.user_id = ". $db->sanitize($this->user_id);
		}
		if ($this->from)
		{
			$query .="
			AND events.event_date >= '". $db->sanitize($this->from->format('Y-m-d')) . "'";
		}
		if ($this->to)
		{
			$query .="
			AND events.event_date <= '". $db->sanitize($this->to->format('Y-m-d')) . "'";
		}
		if ($this->search_text)
		{
			$search_text = $db->sanitize($this->search_text);
			$query .="
			AND (events.title LIKE '%$search_text%'
				OR events.description LIKE '%$search_text%')"; 
		}
		
		$factory=new ModelFactory('Event');
		return $factory->get_by_query($query);
	}
}
?>